<template>
	<view>
		<view v-if="i.goods_price !== 0" class="goods-item" v-for="i in goodsList" :key="i.goods_id" @click="toDetail(i)">
			<!-- 商品图片 -->
			<image :src="i.goods_big_logo || defaultImg"/>
			<!-- 右边view -->
			<view class="goods-info">
				<!-- 商品标题 -->
				<view class="title">{{ i.goods_name }}</view>
				<!-- 商品价格 -->
				<view class="price">￥ {{ i.goods_price.toFixed(2) }}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				defaultImg:'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
				params: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				goodsList: [],
				isLoading: false,
				total: 99999
			}
		},
		onLoad(options) {
			this.params.query = options.query || ''
			this.params.cid = options.cid || ''
			this.getGoodsList()
		},
		onReachBottom() {
			if (this.goodsList.length >= this.total) uni.$showToast('没有更多商品了')
			if(this.isLoading) return
			this.params.pagenum++
			this.getGoodsList()
		},
		onPullDownRefresh() {
			this.params.pagenum = 1
			this.total = 99999
			this.goodsList = []
			this.getGoodsList()
		},
		methods: {
			async getGoodsList() {
				this.isLoading = true
				const { data: res } = await uni.$http.get('goods/search', this.params)
				this.isLoading = false
				uni.stopPullDownRefresh()
				if (res.meta.status !== 200) uni.$showToast()
				this.total = res.message.total
				this.goodsList.push(...res.message.goods)
			},
			toDetail (i) {
				uni.navigateTo({
					// url:'/subpkg/goods_detail/goods_detail?goods_id=' + i.goods_id
					url: `/subpkg/goods_detail/goods_detail?goods_id=${i.goods_id}`
				})
			},
		}
	}
</script>

<style lang="scss">
.goods-item {
	display: flex;
	padding: 20rpx;
	image {
		width: 120px;
		height: 120px;
		border-radius: 6px;
	}
	.goods-info {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		margin-left: 20rpx;
		.title {
			width: 430rpx;
			font-size: 12px;
		}
		.price {
			color: #c00000;
		}
	}
}
</style>
